/********************************************************************/
/*                                                                  */
/*	       CREAM: A (JVM-like) Toy Abstract Machine             */
/*                                                                  */
/*		Copyright (c) 1998- , C.R. Ramakrishnan             */
/*                                                                  */
/********************************************************************/


#include <stdio.h>
#include "machine.h"

void print_string();
void print_int();
void print_float();
void scan_int();
void scan_float();

/* Builtin functions interface to CREAM & SUGAR programs */

void builtin()
{
  switch(stack[--sp])
    {
    case B_PRINT_STR:
      print_string();
      return;

    case B_PRINT_INT:
      print_int();
      return;

    case B_PRINT_FLOAT:
      print_float();
      return;

    case B_SCAN_INT:
      scan_int();
      return;

    case B_SCAN_FLOAT:
      scan_float();
      return;
    }
}

/* The functions themselves are defined using CREAM instructions
   interspersed with C calls */

void print_string()
{
  char* string;
  new_env(1,0);
  ildc(0);
  load();
  string = (char *) &(static_area[region_table[0] + stack[sp-1]]);
  printf("%s", string);
  ret();
}

void print_int()
{
  new_env(1,0);
  ildc(0);
  load();
  printf("%d", stack[sp-1]);
  ret();
}

void print_float()
{
  new_env(1,0);
  ildc(0);
  load();
  printf("%g", i2f(stack[sp-1]));
  ret();
}

void scan_int()
{
  int i;

  new_env(0,0);
  scanf("%d", &i);
  ildc(i);
  ret();
}

void scan_float()
{
  float f;

  new_env(0,0);
  scanf("%f", &f);
  fldc(f);
  ret();
}
